package com.sfeir.client;

import static com.sfeir.client.util.Constants.SQUARE_SIZE;

import java.io.Serializable;

import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.user.client.ui.Image;
import com.sfeir.client.tetriselement.TetrisObject;

public class Case implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7279842089307182885L;

	public static final String BACKGROUND_COLOR = "#DDD";

	private Image image;
	private int height;

	public Case() {
		this.image = null;
		this.height = SQUARE_SIZE;
	}

	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public boolean isFree() {
		return this.image == null;
	}

	public void setCase(TetrisObject object) {
		this.image = object.getBrick();
	}

	public void paint(Context2d ctx, int x, int y) {
		ctx.save();
		if (this.image != null) {
			ctx.drawImage(ImageElement.as(this.image.getElement()), x * SQUARE_SIZE, y * SQUARE_SIZE);
		} else {
			ctx.setFillStyle(BACKGROUND_COLOR);
			ctx.fillRect(x * SQUARE_SIZE, y * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
		}
		ctx.restore();
	}
}
